home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / graphicgems4.lha / GemsIV / arcball / Body.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  2.1 KB  |  78 lines

  1. /***** Body.c *****/
  2. #include <gl/gl.h>
  3. #include "Body.h"
  4.  
  5. enum QuatPart {X, Y, Z, W};
  6. int bodyNPoints = 8;
  7. int bodyNFaces = 7;
  8.  
  9. float theBodyRadius = 3.0;
  10. float thePoints[][4] = {
  11.     { 3.0,     0.0,    0.0,    1},
  12.     {-1.0,     1.0,    0.0,    1},
  13.     {-1.0,    -1.0,    0.0,    1},
  14.     {-0.75,    0.0,    -0.25,    1},
  15.     { 1.0,     0.0,    0.0,    1},
  16.     {-0.75,    0.0,    0.75,    1},
  17.     {-0.5,    -0.125,    0.0,    1},
  18.     {-0.5,     0.125,    0.0,    1}
  19. };
  20. int theFaceVertices[][4] = {
  21.     {3,     0, 1, 2},
  22.     {3,     4, 5, 6},
  23.     {3,     4, 7, 5},
  24.     {3,     5, 7, 6},
  25.     {3,     0, 2, 3},
  26.     {3,     0, 3, 1},
  27.     {3,     1, 3, 2},
  28. };
  29. float theFaceNormals[][4] = {
  30.     {0., 0., 1., 0},
  31.     {0.08152896377979659767, -0.978347565357559172, 0.1902342488195253946, 0},
  32.     {0.08152896377979659767, 0.978347565357559172, 0.1902342488195253946, 0},
  33.     {-0.9486832980505137996, 0., -0.3162277660168379332, 0},
  34.     {0.06428243465332250222, -0.2571297386132900089, -0.9642365197998375334, 0},
  35.     {0.06428243465332250222, 0.2571297386132900089, -0.9642365197998375334, 0},
  36.     {-0.7071067811865475244, 0., -0.7071067811865475244, 0},
  37. };
  38. short theFaceColors[][3] = {
  39.     {102, 204, 255},
  40.     {  0, 153, 204},
  41.     {  0, 153, 204},
  42.     {204,  51, 157},
  43.     { 51, 102, 157},
  44.     { 51, 102, 157},
  45.     {102, 102, 172},
  46. };
  47.  
  48. /* Transform body normals, draw front */
  49. void drawbody(Matrix Rot)
  50. {
  51.     double bodyScale = 1.0/theBodyRadius;
  52.     register int i, j, k, n;
  53.  
  54.     pushmatrix();
  55.     scale(bodyScale, bodyScale, bodyScale);
  56.     for (j=0; j<bodyNFaces; j++) {
  57.     double dot = Rot[X][Z]*theFaceNormals[j][X]
  58.             +Rot[Y][Z]*theFaceNormals[j][Y]
  59.             +Rot[Z][Z]*theFaceNormals[j][Z];
  60.     if (dot>0.0) {      /* Front-facing polygon, so draw it */
  61.         short shadedColor[3];
  62.         dot += 0.4; if (dot>1.0) dot = 1.0;
  63.         shadedColor[0] = dot*theFaceColors[j][0];
  64.         shadedColor[1] = dot*theFaceColors[j][1];
  65.         shadedColor[2] = dot*theFaceColors[j][2];
  66.         n = theFaceVertices[j][0];
  67.         RGBcolor(shadedColor[0], shadedColor[1], shadedColor[2]);
  68.         bgnpolygon();
  69.         for (k=1; k<=n; k++) {
  70.         i = theFaceVertices[j][k];
  71.         v4f(thePoints[i]);
  72.         }
  73.         endpolygon();
  74.     }
  75.     }
  76.     popmatrix();
  77. }
  78.